// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Continue Screen Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timerSeconds
private alias object.value1 : object.timerFrames

private alias 1 : STATE_COUNTING

// Continue Setup states
private alias 2 : CONTINUE_DISPLAY
private alias 4 : CONTINUE_RUN
private alias 6 : CONTINUE_TIMEDOUT


// ========================
// Events
// ========================

event ObjectUpdate
	if object.state == STATE_COUNTING
		object.timerFrames--

		if object.timerFrames <= 0
			if object.timerSeconds == 0
				// Make this object stop counting
				object.state++

				// Tell the main object that time is up
				object[+1].state = CONTINUE_TIMEDOUT
			else
				// Time's not up yet, progress to the next second
				object.timerSeconds--
				object.timerFrames = 60
			end if
		end if
	end if
end event


event ObjectDraw
	// Draw the constant sprites ("Continue" & stars)
	DrawSprite(0)
	DrawSprite(1)

	// Draw the countdown
	temp0 = object.xpos
	temp0 >>= 16
	DrawNumbers(2, temp0, 126, object.timerSeconds, 2, 8, true)

	// Draw the continue line of mini-Sonics

	// First find the base X pos to draw them at
	temp0 = player.continues
	temp1 = temp0
	temp0 -= 2
	temp0 *= 21
	temp0 >>= 1
	FlipSign(temp0)
	temp0 += screen.xcenter

	// Then get the sprite to use
	GetBit(object.frame, oscillation, 4)
	object.frame += 12

	temp3 = true

	// (temp1 is number of mini-Sonics to draw)
	while temp1 > 1
		temp2 = true

		if temp3 == true
			temp3 = false
			if object[+1].state >= CONTINUE_RUN
				if object[+1].state != CONTINUE_TIMEDOUT
					// Don't draw
					temp2 = false
				end if
			else
				if object[+1].state > CONTINUE_DISPLAY
					// Flash
					GetBit(temp2, oscillation, 0)
				end if
			end if
		end if

		if temp2 == true
			DrawSpriteScreenXY(object.frame, temp0, 100)
		end if

		temp0 += 21
		temp1--
	loop
end event


event ObjectStartup
	LoadSpriteSheet("Continue/Objects.gif")

	// #0 - "CONTINUE" text
	SpriteFrame(-85, 64, 171, 16, 1, 1)

	// #1 - Stars
	SpriteFrame(-21, 128, 41, 9, 1, 42)

	// #2 -> #11 - Numbers
	SpriteFrame(0, 0, 8, 11, 1, 52)
	SpriteFrame(0, 0, 8, 11, 10, 52)
	SpriteFrame(0, 0, 8, 11, 19, 52)
	SpriteFrame(0, 0, 8, 11, 28, 52)
	SpriteFrame(0, 0, 8, 11, 37, 52)
	SpriteFrame(0, 0, 8, 11, 46, 52)
	SpriteFrame(0, 0, 8, 11, 55, 52)
	SpriteFrame(0, 0, 8, 11, 64, 52)
	SpriteFrame(0, 0, 8, 11, 73, 52)
	SpriteFrame(0, 0, 8, 11, 82, 52)

	// Character icons are dependant on the main player
	switch stage.playerListPos
	case PLAYER_SONIC_A
	case PLAYER_SONIC_TAILS_A
		// #12 -> #13 - Sonic foot tapping
		SpriteFrame(-8, -11, 16, 23, 1, 18)
		SpriteFrame(-8, -11, 16, 23, 18, 18)
		break
		
	case PLAYER_TAILS_A
		// #12 -> #13 - Tails tapping his foot
		SpriteFrame(-8, -11, 16, 23, 35, 18)
		SpriteFrame(-8, -11, 16, 23, 52, 18)
		break
		
	case PLAYER_KNUCKLES_A
#platform: USE_ORIGINS
	case PLAYER_KNUCKLES_TAILS_A
#endplatform
		// #12 -> #13 - Knuckles tapping his foot (he has a hole in his feet!)
		SpriteFrame(-8, -11, 16, 23, 69, 18)
		SpriteFrame(-8, -11, 16, 23, 86, 18)
		break
		
#platform: USE_ORIGINS
	case PLAYER_AMY_A
	case PLAYER_AMY_TAILS_A
		// #12 -> #13 - Amy tapping her foot
		SpriteFrame(-8, -11, 17, 22, 103, 18)
		SpriteFrame(-8, -11, 17, 22, 103, 41)
		break
#endplatform
	end switch

	foreach (TypeName[Continue Screen], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder 	= 5
		object[arrayPos0].xpos 			= screen.xcenter
		object[arrayPos0].xpos 			<<= 16
		object[arrayPos0].ypos 			= 0
		object[arrayPos0].timerSeconds 	= 10
		object[arrayPos0].timerFrames 	= 60
	next
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Continue/Objects.gif")
	SpriteFrame(-59, 64, 119, 16, 1, 1)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
